<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class ReportReg extends Controller {
    public function __construct() {
        parent::__construct ();
        $this->_data = array ();
        $this->load->model ( 'pasien_model' );
        isController('registrasi','reportreg');
    }
    protected function _view($template, $data = array(), $result = false) {
        if ($data && is_array ( $data )) {
            return $this->load->view ( $template, $data, $result );
        }
        return $this->load->view ( $template, $this->_data, $result );
    }
    public function index() {
        redirect ( 'klinik/reportReg/regBaru' );
    }

    public function regBaru() {
    	if (! isAccess ( 'klinik', 'reportreg', 'regbaru'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'frdate', 'todate', 'action', 'page' ) );
        $array['page'] = (int) $array['page'];
        
        //Uri
        $frdate = $this->input->post('frdate') ? $this->input->post('frdate') : 'all';
        $array['frdate'] = get_data($array,'frdate') ? get_data($array,'frdate') : $frdate;
        
		$todate = $this->input->post('todate') ? $this->input->post('todate') : 'all';        
        $array['todate'] = get_data($array,'todate') ? get_data($array,'todate') : $todate;

        //Where
        $where = array();
        $where['pasienBaru'] = 1;
        if( $array['frdate'] != 'all' )
        	$where['frdate'] = $array['frdate'];
        if( $array['todate'] != 'all' )
        	$where['todate'] = $array['todate'];	
    	
        $total = $this->pasien_model->getCountPasien($where);
        $this->load->library('pagination');
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
        $limit = $this->config->item('klinik_limit');
        $config = $this->config->item('paging');
       	$config['base_url'] = site_url('klinik/reportReg/regBaru/frdate/'.$array['frdate'].'/todate/'.$array['todate'].'/page/');
        $config['uri_segment'] = 8;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
        $dataView = $this->pasien_model->getListPasien($limit, $array['page'], $where);
        $this->_data['dataView'] = $dataView;
        
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( 'klinik/reportRegBaru' );
    }
    
	public function printBaru() {
    	if (! isAccess ( 'klinik', 'reportreg', 'printbaru'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('frdate', 'todate') );
        if ( !$array ['frdate'] || !$array ['todate'] )
            show_404 ();
            
	    //Data
        $where = array();
        $where['pasienBaru'] = 1;
        if( $array['frdate'] != 'all' )
        	$where['frdate'] = $array['frdate'];
        if( $array['todate'] != 'all' )
        	$where['todate'] = $array['todate'];	
        	
        $dataView = $this->pasien_model->getListPasien(100, 0, $where);
	    
        //Template PDF
        $content = '<page backtop="14mm" backbottom="14mm" pagegroup="new">
				    <page_header>
					<table style="width:100%;">
					    <tr>
							<td style="width:70%;">'.$this->lang->line('lap_pdf_title').'</td>
							<td style="width:30%;">Laporan Pasien Baru</td>
					    </tr>
				        <tr>
							<td>'.$this->lang->line('global_title_cab').'</td>';
        
						if ($array['frdate'] != 'all' && $array['todate'] == 'all') {	
							$content .= '<td>Tanggal : '.date('d M Y', strtotime($array['frdate'])).'</td>';
						} elseif ($array['frdate'] != 'all' && $array['todate'] != 'all') {	
							$content .= '<td>Tanggal : '.date('d M Y', strtotime($array['frdate'])).' - '.date('d M Y', strtotime($array['todate'])).'</td>';
						} else {
							$content .= '<td>Semua Pasien</td>';
						}
						
			$content .= '</tr>
					    <tr>
							<td>'.$this->lang->line('global_alamat_cab').'</td>
							<td></td>
					    </tr>
					</table>
				    </page_header>
				    <page_footer>
					<table style="width: 100%;">
					    <tr>
						<td style="text-align: right">
						    Page : [[page_cu]]/[[page_nb]]
						</td>
					    </tr>
					</table>
				    </page_footer>
				</page><br />';
	
	    $content .= '<table border="1" style="width:100%;">
				    <thead style="text-align:center;height:100px;">
					<tr height="50">
						<th style="width:5%">No</th>
					    <th style="width:10%">Tanggal</th>
	                    <th style="width:10%">Waktu</th>
	                    <th style="width:30%">Nama</th>
	                    <th style="width:10%">Jenkel</th>
	                    <th style="width:10%">Usia</th>
	                    <th style="width:15%">No Telp</th>
	                    <th style="width:10%">Jml</th>
					</tr>
				    </thead>
				    <tbody style="text-align:center;">';

	    $no=1;
	    if($dataView) foreach ($dataView as $key=>$row) {
			$content .= '<tr>
							<td>'.$no.'</td>';
							
							if( $key > 0 ) {
								if( mydate($dataView[$key-1]['pasienLastRJ']) == mydate($row['pasienLastRJ']) ) {
									$content .= '<td></td>';
								} else {
									$content .= '<td>'.mydate($row['pasienLastRJ']).'</td>';
								}
								
							} else {
								$content .= '<td>'.mydate($row['pasienLastRJ']).'</td>';
							}
							
				$content .= '<td>'.mytime($row['pasienLastRJ']).'</td>
							 <td align="left">'.$row['pasienNama'].'</td>
							 <td>'.$row['pasienJenKel'].'</td>
							 <td>'.getUsia(get_data($row,'pasienTglLahir')).'</td>
							 <td>'.$row['pasienNoTelp'].'</td>
							 <td>'.$row['pasienJmlRJ'].' Kali</td>
						 </tr>';
			$no++;
	    }
	   
	    $content .= '</tbody>
	                 </table>';
		
	    //Convert to PDF
	    $this->load->library('html2pdf/html2pdf');
	    $html2pdf = new HTML2PDF('L', 'A4');
	    $html2pdf->pdf->SetDisplayMode('fullpage');
	    $html2pdf->writeHTML($content, FALSE);
	    
		if ($array['frdate'] != 'all' && $array['todate'] == 'all') {	
			$html2pdf->Output( 'Laporan_Pasien_Baru_'.date('dmY', strtotime($array['frdate'])).'.pdf' );
		} elseif ($array['frdate'] != 'all' && $array['todate'] != 'all') {	
			$html2pdf->Output( 'Laporan_Pasien_Baru_'.date('dmY', strtotime($array['frdate'])).'_'.date('dmY', strtotime($array['todate'])).'.pdf' );
		} else {
			$html2pdf->Output( 'Laporan_Pasien_Baru_Semua.pdf' );
		}
		
    }
    
	public function regMember() {
    	if (! isAccess ( 'klinik', 'reportreg', 'regmember'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'frdate', 'todate', 'action', 'page' ) );
        $array['page'] = (int) $array['page'];
        
        //Uri
        $frdate = $this->input->post('frdate') ? $this->input->post('frdate') : 'all';
        $array['frdate'] = get_data($array,'frdate') ? get_data($array,'frdate') : $frdate;
        
		$todate = $this->input->post('todate') ? $this->input->post('todate') : 'all';        
        $array['todate'] = get_data($array,'todate') ? get_data($array,'todate') : $todate;

        //Where
        $where = array();
        $where['pasienMember'] = 2;
        if( $array['frdate'] != 'all' )
        	$where['frdate'] = $array['frdate'];
        if( $array['todate'] != 'all' )
        	$where['todate'] = $array['todate'];	

        $total = $this->pasien_model->getCountPasien($where);
        $this->load->library('pagination');
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
        $limit = $this->config->item('klinik_limit');
        $config = $this->config->item('paging');
       	$config['base_url'] = site_url('klinik/regMember/regBaru/frdate/'.$array['frdate'].'/todate/'.$array['todate'].'/page/');
        $config['uri_segment'] = 8;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
        $dataView = $this->pasien_model->getListPasien($limit, $array['page'], $where);
        $this->_data['dataView'] = $dataView;
        $this->_data['uri_to_assoc'] = $array;

        $this->_view ( 'klinik/reportRegMember' );
    }
    
	public function printMember() {
    	if (! isAccess ( 'klinik', 'reportreg', 'printbaru'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('frdate', 'todate') );
        if ( !$array ['frdate'] || !$array ['todate'] )
            show_404 ();

	    //Data
        $where = array();
        $where['pasienMember'] = 2;
        if( $array['frdate'] != 'all' )
        	$where['frdate'] = $array['frdate'];
        if( $array['todate'] != 'all' )
        	$where['todate'] = $array['todate'];	
        	
        $dataView = $this->pasien_model->getListPasien(100, 0, $where);
	    
        //Template PDF
        $content = '<page backtop="14mm" backbottom="14mm" pagegroup="new">
				    <page_header>
					<table style="width:100%;">
					    <tr>
							<td style="width:70%;">'.$this->lang->line('global_title').'</td>
							<td style="width:30%;">Laporan Pasien Ulang</td>
					    </tr>
				        <tr>
							<td>'.$this->lang->line('global_title_cab').'</td>';
        
						if ($array['frdate'] != 'all' && $array['todate'] == 'all') {	
							$content .= '<td>Tanggal : '.date('d M Y', strtotime($array['frdate'])).'</td>';
						} elseif ($array['frdate'] != 'all' && $array['todate'] != 'all') {	
							$content .= '<td>Tanggal : '.date('d M Y', strtotime($array['frdate'])).' - '.date('d M Y', strtotime($array['todate'])).'</td>';
						} else {
							$content .= '<td>Semua Pasien</td>';
						}
						
			$content .= '</tr>
					    <tr>
							<td>'.$this->lang->line('global_alamat_cab').'</td>
							<td></td>
					    </tr>
					</table>
				    </page_header>
				    <page_footer>
					<table style="width: 100%;">
					    <tr>
						<td style="text-align: right">
						    Page : [[page_cu]]/[[page_nb]]
						</td>
					    </tr>
					</table>
				    </page_footer>
				</page><br />';
	
	    $content .= '<table border="1" style="width:100%;">
				    <thead style="text-align:center;height:100px;">
					<tr height="50">
						<th style="width:5%">No</th>
					    <th style="width:10%">Tanggal</th>
	                    <th style="width:10%">Waktu</th>
	                    <th style="width:30%">Nama</th>
	                    <th style="width:10%">Jenkel</th>
	                    <th style="width:10%">Usia</th>
	                    <th style="width:15%">No Telp</th>
	                    <th style="width:10%">Jml</th>
					</tr>
				    </thead>
				    <tbody style="text-align:center;">';

	    $no=1;
	    if($dataView) foreach ($dataView as $key=>$row) {
			$content .= '<tr>
							<td>'.$no.'</td>';
							
							if( $key > 0 ) {
								if( mydate($dataView[$key-1]['pasienLastRJ']) == mydate($row['pasienLastRJ']) ) {
									$content .= '<td></td>';
								} else {
									$content .= '<td>'.mydate($row['pasienLastRJ']).'</td>';
								}
								
							} else {
								$content .= '<td>'.mydate($row['pasienLastRJ']).'</td>';
							}
							
				$content .= '<td>'.mytime($row['pasienLastRJ']).'</td>
							 <td align="left">'.$row['pasienNama'].'</td>
							 <td>'.$row['pasienJenKel'].'</td>
							 <td>'.getUsia(get_data($row,'pasienTglLahir')).'</td>
							 <td>'.$row['pasienNoTelp'].'</td>
							 <td>'.$row['pasienJmlRJ'].' Kali</td>
						 </tr>';
			$no++;
	    }
	   
	    $content .= '</tbody>
	                 </table>';
		
	    //Convert to PDF
	    $this->load->library('html2pdf/html2pdf');
	    $html2pdf = new HTML2PDF('L', 'A4');
	    $html2pdf->pdf->SetDisplayMode('fullpage');
	    $html2pdf->writeHTML($content, FALSE);
	    
		if ($array['frdate'] != 'all' && $array['todate'] == 'all') {	
			$html2pdf->Output( 'Laporan_Pasien_Baru_'.date('dmY', strtotime($array['frdate'])).'.pdf' );
		} elseif ($array['frdate'] != 'all' && $array['todate'] != 'all') {	
			$html2pdf->Output( 'Laporan_Pasien_Ulang_'.date('dmY', strtotime($array['frdate'])).'_'.date('dmY', strtotime($array['todate'])).'.pdf' );
		} else {
			$html2pdf->Output( 'Laporan_Pasien_Ulang_Semua.pdf' );
		}
		
    }
    
}    